<?php

namespace App\Models;

use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Tymon\JWTAuth\Contracts\JWTSubject;
use Hash;

class User extends Authenticatable implements JWTSubject
{
    use SoftDeletes;
    use Notifiable;

    protected $table = 'love_users';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'mobile', 'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    /**
     * Automatically creates hash for the user password.
     *
     * @param string $value
     */
    public function setPasswordAttribute($value)
    {
        $this->attributes['password'] = Hash::make($value);
    }

    public function getJWTIdentifier()
    {
        return $this->getKey();
    }

    public function getJWTCustomClaims()
    {
        return [];
    }






    public static function boot()
    {
        parent::boot();

        static::deleted(function ($model)
        {
            Points::where('user_id', $model->id)->delete();
            \App\Models\Department\User::where('user_id', $model->id)->delete();
        });
    }

    public function scopeAll()
    {
        return $this->where('id', '>', 0);
    }

    public function department()
    {
        return $this->belongsToMany(Department::Class, 'love_department_users');
    }

    public function points()
    {
        return $this->hasOne(Points::Class);
    }

    public function familyUser()
    {
        return $this->hasOne(\App\Models\Family\User::Class);
    }

    public function family()
    {
        return $this->hasOne(\App\Models\Family\User::Class)
            ->with('family');
    }
}
